<template>
  <el-dialog
    :visible.sync="isShow"
    title="新增员工"
    width="600px"
    @close="CloseEvent"
  >
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" />
      </el-form-item>
      <el-form-item label="入职方式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employeesData.hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <div v-outClick="treeCloseEvent">
          <el-input
            v-model="form.departmentName"
            readonly
            @focus="focusEvent"
          />
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="CloseEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employeesData from '@/api/constant/employees'
import { companydepartment } from '@/api/department'
import { sysUserPost } from '@/api/human'

export default {
  data() {
    return {
      isShow: false,
      employeesData,
      treeShow: false,
      treeData: [],
      form: {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 1, max: 11, message: '请填入11位电话号码', trigger: 'change' }
        ],
        timeOfEntry: [{ required: true, message: '必填', trigger: 'change' }],
        formOfEmployment: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        workNumber: [{ required: true, message: '必填', trigger: 'change' }],
        departmentName: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        correctionTime: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },

  methods: {
    async focusEvent() {
      this.treeShow = true
      if (this.treeData.length === 0) {
        const res = await companydepartment()
        console.log('验证信息', res)
        this.treeData = this.changeData(res.data.depts, '')
      }
    },
    nodeClick(data) {
      this.treeShow = false
      this.form.departmentName = data.name
    },
    changeData(arr, pid = '') {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.isShow = false
          this.$message.success('添加成功')
          this.$emit('getData')
        }
      })
    },
    treeCloseEvent() {
      this.treeShow = false
    },
    CloseEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        timeOfEntry: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        correctionTime: ''
      }
    }
  }
}
</script>

<style></style>
